package com.eet.kafka.producer;

import com.eet.kafka.constants.CommonConstants;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Kafka生产者
 *
 * @author Administrator
 */
@RestController
@AllArgsConstructor
public class KafkaProducer {
    private final KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 发送消息
     *
     * @param message 消息体
     */
    @GetMapping("/kafka/normal/{size}/{message}")
    public ResponseEntity<String> sendMessage(@PathVariable("message") String message, @PathVariable("size") int size) throws Exception {
        for (int i = 0; i < size; i++) {
            kafkaTemplate.send(CommonConstants.TEST_TOPIC, message);
        }
        return ResponseEntity.ok("发送成功");
    }
}
